<template>
  <div class="app-container-tab c-width c-dis-flex-row c-hidden">
    <el-row :gutter="20" class="c-flex c-dis-flex-row c-hidden">
      <el-col :span="6" :xs="24" class="c-flex c-dis-flex-col c-hidden">
        <div class="com-tabs-buttons">
          <el-button
            type="primary"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete"
            >删除</el-button
          >
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </div>
        <div class="head-container">
          <el-input
            v-model="categoryName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 10px"
          />
        </div>
        <div class="com_tree_list c-flex c-scroll-y">
          <el-tree
            :data="categoryOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col
        :span="18"
        :xs="24"
        class="c-dis-flex-col c-hidden com_tree_right_bg"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="100px"
          class="c-flex c-scroll-y"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="分类编码" prop="categoryCode">
                <el-input
                  v-model="form.categoryCode"
                  placeholder="请输入分类编码"
                  maxlength="30"
                  disabled
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分类名称" prop="categoryName">
                <el-input
                  v-model="form.categoryName"
                  placeholder="请输入分类名称"
                  maxlength="30"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="上级分类" prop="parentId">
                <treeselect
                  no-options-text="无"
                  v-model="form.parentId"
                  :options="categoryOptions"
                  :show-count="true"
                  placeholder="请选择上级分类"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序号" prop="orderNum">
                <el-input-number
                  style="width: 100%"
                  v-model="form.orderNum"
                  placeholder="请输入排序号"
                  @change="toInteger"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-col :span="24" class="com_form_btn_center">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button @click="cancel">取 消</el-button>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  categoryTreeSelect,
  getCategory,
  delCategory,
  addCategory,
  updateCategory,
} from "@/api/questionbank/category";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Cateogry",
  components: { Treeselect },
  data() {
    return {
      // 弹出层标题
      title: "",
      // 题库分类选项
      categoryOptions: undefined,
      // 分类名称
      categoryName: undefined,
      // 表单参数
      form: {
        categoryId: undefined,
        categoryCode: undefined,
        categoryName: undefined,
        parentId: undefined,
        orderNum: undefined,
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        categoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
          { max: 20, message: "分类名称长度必须小于30个字符", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "排序号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    // 根据分类名称筛选分类树
    categoryName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getCategoryTree();
  },
  methods: {
    toInteger() {
      let reg = /^[0-9]+$/;
      if (!reg.test(this.form.orderNum)) {
        this.$message.warning("只能输入整数排序");
        // 用以在dom渲染挂载后重新触发dom渲染挂载
        this.$nextTick(() => {
          this.form.orderNum = parseInt(this.form.orderNum);
        });
      }
    },
    /** 查询题库分类树结构 */
    getCategoryTree() {
      categoryTreeSelect().then((response) => {
        this.categoryOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.categoryId = data.id;
      getCategory(data.id).then((response) => {
        this.form = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        categoryId: undefined,
        parentId: undefined,
        categoryCode: undefined,
        categoryName: undefined,
        orderNum: undefined,
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.categoryId != undefined) {
            updateCategory(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.getCategoryTree();
            });
          } else {
            addCategory(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.getCategoryTree();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete() {
      if (this.form.categoryId == undefined) {
        this.$modal.msgError("请先选择要删除的节点!");
        return;
      }
      const categoryId = parseInt(this.form.categoryId);
      this.$modal.confirm(
          '是否确认删除"' + this.form.categoryName + '"的数据项？'
        ).then(function () {
          return delCategory([categoryId]);
        }).then(() => {
          this.getCategoryTree();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
          this.$modal.msg('已取消删除');
      });
    },
  },
};
</script>
